package com.huluobo.a2110mvpimdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.a2110mvpimdemo.R;
import com.huluobo.a2110mvpimdemo.chat.ChatActivity;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.util.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by LC on 2023/12/22.
 */
public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ConversationItemViewHolder> {
    private List<EMConversation> conversations;
    private Context context;

    public ConversationAdapter(List<EMConversation> conversations, Context context) {
        this.conversations = conversations;
        this.context = context;
    }

    @NonNull
    @Override
    public ConversationItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ConversationItemViewHolder(
                LayoutInflater.from(parent.getContext()).inflate(R.layout.item_conversation, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ConversationItemViewHolder holder, int position) {
        //会话列表的用户名/群名
        holder.username.setText(conversations.get(position).conversationId());
        //最后一条消息
        if (conversations.get(position).getLastMessage().getType() == EMMessage.Type.TXT) {
            //证明它是一条文本消息
            EMTextMessageBody body = (EMTextMessageBody) conversations.get(position).getLastMessage().getBody();
            holder.lastMessage.setText(body.getMessage());
        } else if (conversations.get(position).getLastMessage().getType() == EMMessage.Type.IMAGE) {
            holder.lastMessage.setText("[图片]");
        } else if (conversations.get(position).getLastMessage().getType() == EMMessage.Type.FILE) {
            holder.lastMessage.setText("[文件]");
        } else {
            holder.lastMessage.setText("其他消息");
        }
        //最后一条消息的时间戳
        //conversations.get(position).getLastMessage().getMsgTime() 获取最后一条消息的时间,当前是一个long
        //通过系统的newDate 转换为一个时间格式
        // 再通过环信的DateUtils.getTimestampString转换为一个String格式
        String timestampString =
                DateUtils.getTimestampString(new Date(conversations.get(position).getLastMessage().getMsgTime()));
        holder.timestamp.setText(timestampString);
        //未读消息的数量
        if (conversations.get(position).getUnreadMsgCount() > 0) {
            holder.unreadCount.setVisibility(View.VISIBLE);
            holder.unreadCount.setText(String.valueOf(conversations.get(position).getUnreadMsgCount()));
        } else {
            holder.unreadCount.setVisibility(View.GONE);
        }

        holder.layout.setOnClickListener(v -> {
            String conversationId = conversations.get(position).conversationId();
            int chatType = 0;
            switch (conversations.get(position).getLastMessage().getChatType()) {
                case Chat:
                    chatType = 1;
                    break;
                case GroupChat:
                    chatType = 2;
                    break;
                case ChatRoom:
                    chatType = 3;
                    break;
            }
            Intent intent = new Intent(context, ChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            bundle.putInt("chatType", chatType);
            intent.putExtras(bundle);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return conversations.size();
    }

    static class ConversationItemViewHolder extends RecyclerView.ViewHolder {
        TextView username, lastMessage, timestamp, unreadCount;
        RelativeLayout layout;

        public ConversationItemViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.item_conversation_username);
            lastMessage = itemView.findViewById(R.id.item_conversation_lastMessage);
            timestamp = itemView.findViewById(R.id.item_conversation_timestamp);
            unreadCount = itemView.findViewById(R.id.item_conversation_unreadCount);
            layout = itemView.findViewById(R.id.item_conversation_rl);
        }
    }
}
